﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace System
{
	public partial class GraphForm : Form
	{
		enum graphmode
		{
			none,
			createpoint,
			addlink
		}
		graphmode mode = graphmode.none;
		GraphPoint<V3D,double> selectedpoint = null;	
		GraphPoint<V3D,double> clickpoint = null;
		Graph<V3D,double> Graph = new Graph<V3D,double>();
		void DrawGraph()
		{
			Color[] C = new Color[] { Color.Red, Color.Green, Color.Blue, Color.Yellow, Color.Black, Color.Lime, Color.Magenta, Color.Orange };
			if (BackgroundImage == null)
				BackgroundImage = new Bitmap(ClientSize.Width, ClientSize.Height);
			Graphics G = Graphics.FromImage(BackgroundImage);
			G.Clear(Color.White);
			foreach (var P in Graph.Points)
			{/**
				MathWin32.FillCircle(G, P == selectedpoint ? Brushes.Gold : new SolidBrush(C[P.Dij_Group]), P.Content, 5);
				foreach (var a in P.Adjacents)
				{
					PointF P1 = P.Content.ToPointF();
					PointF P2 = a.Key.Content.ToPointF();
					G.DrawLine(Pens.Blue, P1, P2);
				}*/
			}
			G.Flush();
			G.Dispose();
			Refresh();
		}
		public GraphForm()
		{
			InitializeComponent();
		}

		private void addPointsToolStripMenuItem_Click(object sender, EventArgs e)
		{
			mode = graphmode.createpoint;
		}

		private void createLinksToolStripMenuItem_Click(object sender, EventArgs e)
		{
			mode = graphmode.addlink;
		}

		private void loadGraphToolStripMenuItem_Click(object sender, EventArgs e)
		{
			/**Graph.Load();*/
			DrawGraph();
		}

		private void saveGraphToolStripMenuItem_Click(object sender, EventArgs e)
		{
			/**Graph.Save();*/
		}
		protected override void OnKeyDown(KeyEventArgs e)
		{
			if (e.KeyCode == Keys.LShiftKey)
				GraphForm_KeyDown(this, e);
			else
				base.OnKeyDown(e);
		}
		private void GraphForm_Click(object sender, EventArgs e)
		{
			clickpoint = null;
			double d = 5;
			double D;
			V3D cursor = new V3D(PointToClient(Cursor.Position).X, PointToClient(Cursor.Position).Y, 0);
			for (int i = 0; i < Graph.Points.Count; i++)
			{
				if ((D = (cursor - Graph.Points[i].Content).Length) < d)
				{
					d = D;
					clickpoint = Graph.Points[i];
				}
			}
			if (mode == graphmode.createpoint)
			{
				if (ctrl)
				{
					if (clickpoint != null)
						Graph.Delete(clickpoint);
				}
				else
				{
					Point P = PointToClient(Cursor.Position);
					Graph.AddPoint(new V3D(P.X, P.Y, 0));
				}
			}
			else if (mode == graphmode.addlink)
			{
				if (shift)
				{
					selectedpoint = clickpoint;
				}
				else
				{
					if (selectedpoint != null && clickpoint != null && selectedpoint != clickpoint)
					{
						if (ctrl)
							selectedpoint.UnBind(clickpoint);
						else
						{
							Graph.Bind(clickpoint, selectedpoint, (selectedpoint.Content - clickpoint.Content).Length);
						}
					}
				}
			}
			DrawGraph();
		}
		bool shift = false;
		bool ctrl = false;
		private void GraphForm_KeyDown(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.ShiftKey)
				shift = true;
			else if (e.KeyCode == Keys.ControlKey)
				ctrl = true;
		}

		private void GraphForm_KeyUp(object sender, KeyEventArgs e)
		{
			if (e.KeyCode == Keys.ShiftKey)
				shift = false;
			else if (e.KeyCode == Keys.ControlKey)
				ctrl = false;
		}
	}
}